﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Desaware.MachineLicense40;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        CodeEntryControl co;
        public Form1()
        {
            InitializeComponent();
             co = new CodeEntryControl();
            co.LicenseCodeEntered += Co_LicenseCodeEntered;
            this.Controls.Add(co);
        }

        private void Co_LicenseCodeEntered(object Sender, CodeEntryEventArgs e)
        {
            Console.WriteLine(co.InstallCode);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmActivationCodeEntry frm = new frmActivationCodeEntry();

            new Main().Run();
        }
        private string ProcessNewActivationCode()
        {
            bool result = false;
            string _errorMessage = string.Empty;
            string licenseLocation = LicensePath.PathToCertificate();
            DesawareInterface desawareInterface = new DesawareInterface(_desawareServerTimeout);
            ClientLicense desawareClientLicense = desawareInterface.GetDesawareClientLicense();
            int num = 0;
            bool flag = false;
            do
            {
                _errorMessage = string.Empty;
                if (num >= 6)
                {
                    flag = true;
                    break;
                }
                frmActivationCodeEntry frmActivationCodeEntry2 = ((num >= 0) ? new frmActivationCodeEntry(desawareClientLicense, displayCopyLicenseButton: true, _fontName, _fontSize, _fontCharSet, _languageFile) : new frmActivationCodeEntry(desawareClientLicense, displayCopyLicenseButton: false, _fontName, _fontSize, _fontCharSet, _languageFile));
                _licenseIsGood = false;
                frmActivationCodeEntry2.OptionClicked += theEntryForm_OptionClicked;
                DialogResult dialogResult = frmActivationCodeEntry2.ShowDialog();
                frmActivationCodeEntry2.OptionClicked -= theEntryForm_OptionClicked;
                if (dialogResult == DialogResult.OK)
                {
                    try
                    {
                        bool codeReUseWarning = false;
                        if (desawareInterface.InstallClientLicense(frmActivationCodeEntry2.ActivationCode, licenseLocation, out var errorMessage, AllowDeferred: false, suppressMessages: false, ref codeReUseWarning) == InstallClientLicenseResults.LicenseIsGood)
                        {
                            result = true;
                            flag = true;
                        }
                        else
                        {
                            _errorMessage = errorMessage;
                        }
                    }
                    catch (Exception ex)
                    {
                        _errorMessage = ex.Message;
                        if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))
                        {
                            _errorMessage = _errorMessage + Environment.NewLine + ex.InnerException.Message;
                        }
                    }
                    if (!string.IsNullOrEmpty(_errorMessage))
                    {
                        MessageBox.Show(_errorMessage, _languageFile.GetLangString(20), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                }
                else
                {
                    result = _licenseIsGood;
                    flag = true;
                }
                num++;
            }
            while (!flag);
            return result;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ManagementClass managementClass = new ManagementClass("Win32_Processor");
            ManagementObjectCollection instances = managementClass.GetInstances();
            string text = null;
            string text2 = null;
            string text3 = null;
            string text4 = null;
            using (ManagementObjectCollection.ManagementObjectEnumerator managementObjectEnumerator = instances.GetEnumerator())
            {
                if (managementObjectEnumerator.MoveNext())
                {
                    ManagementObject managementObject = (ManagementObject)managementObjectEnumerator.Current;
                    text = managementObject.Properties["ProcessorId"].Value.ToString();
                }
            }
            string text5 = "";
            string text6 = "";
            string text7 = "";
            string text8 = "";
            string text9 = "";
            ManagementObject managementObject2 = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
            text5 = managementObject2.Properties["VolumeSerialNumber"].Value.ToString();
            text2 = text.Substring(10, 2);
            text3 = text.Substring(12, 2);
            text4 = text.Substring(14, 2);
            text6 = text5.Substring(0, 2);
            text7 = text5.Substring(2, 2);
            text8 = text5.Substring(4, 2);
            text9 = text5.Substring(6, 2);
            textBox1.Text = $"{text6 + text2 + text7 + text3 + text8 + text4 + text9}";
        }
    }
}
